export default {
  loginSuccess (state, user) {
    // console.log('etrywteuirweut')
    state.user = {
      ...user,
      isLogin: true
    }
  },
  addToCart (state, item) {
    // console.log(item)
    const {
      id
    } = item
    const isInCart = state.cart.some((cartItem) => {
      return cartItem.id === id
    })
    // console.log(isInCart)
    console.log(state.cart)
    if (isInCart) {
      this.commit('addCartItem', id)
    } else {
      state.cart.push({
        ...item,
        count: 1,
        isChecked: false
      })
    // console.log(55)
    }
  },
  addCartItem (state, id) {
    state.cart = state.cart.map(item => {
      if (item.id === id) {
        item.count++
      }
      return item
    })
  },
  deleteitem (state, id) {
    // console.log(id)
    state.cart.map(item => {
      if (item.id === id) {
        state.cart = state.cart.filter(item =>
          item.id !== id
        )
      }
    })
  },
  toggleItemChecked (state, id) {
    state.cart = state.cart.map(item => {
      if (item.id === id) {
        item.isChecked = !item.isChecked
      }
      return item
    })
  },
  toggleCheckAll (state, e) {
    const { checked } = e.target
    // const checked = !this.getters.isAllChecked
    state.cart = state.cart.map(item => {
      item.isChecked = checked
      return item
    })
  },
  reduceCartItem (state, id) {
    state.cart = state.cart.map(item => {
      if (item.id === id) {
        item.count--
      }
      return item
    })
  },
  outlogin (state, user) {
    // window.localStorage.removeItem('login-user')
    // state.user=
    console.log(12)
    state.user = {
      ...user,
      isLogin: false
    }
  }
}
